'use strict';

define(['controllers', '../service/statService', './filterController', 'highcharts'], function (controllers) {
    controllers.stat.register('StatusController', ['$scope', '$rootScope', 'StatService', function ($scope, $rootScope, statService) {
        $scope.onlyNode = false;

        $scope.STATUS_LEADER = 'LEADER';
        $scope.STATUS_ACTIVE = 'ACTIVE';
        $scope.STATUS_RECOVERING = 'RECOVERING';
        $scope.STATUS_DOWN = 'DOWN';
        $scope.STATUS_RECOVERY_FAILED = 'RECOVERY_FAILED';
        $scope.STATUS_GONE = 'GONE';

        $scope.reloadChart = function(divId, title, stat, width) {
            var series = [];
            for (var i = 0; i < stat.series.length; i++) {
                var serie = {
                    name: stat.series[i].name,
                    data: []
                };
                for (var j = 0; j < stat.categories.length; j++) {
                    serie.data.push(stat.series[i].data[stat.categories[j]]);
                }
                series.push(serie);
            }
            return new Highcharts.Chart({
                chart: {
                    type: 'column',
                    renderTo: divId,
                    width: width
                },
                title: {
                    text: title,
                    x: -20
                },
                colors: ['#007fcc', '#57A957', '#d5dd00', '#c48f00', '#C43C35',  '#e0e0e0'],
                xAxis: {
                    categories: stat.categories
                },
                yAxis: {
                    title: {
                        text: '状态'
                    }
                },
                plotOptions: {
                    column: {
                        stacking: 'percent'
                    }
                },
                tooltip: {
                    shared: true,
                    pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>'
                },
                series: series
            });
        };

        $scope.getStat = function (param) {
            $scope.param = param;
            statService.getStatusStat(param, function (stat) {
                //数据图表
                var chartWidth = Math.max($("#chart_panel").width(), $('#list_panel').width());
                if ($scope.chart != null) {
                    $scope.chart.destroy();
                }
                $scope.chart = $scope.reloadChart('chart', '数据增长统计', stat, chartWidth);

                //数据列表
                var list = {};
                for (var i = 0; i < stat.categories.length; i++) {
                    var categorie = stat.categories[i];
                    list[categorie] = {};
                    for (var j = 0; j < stat.series.length; j++) {
                        var serie = stat.series[j];
                        list[categorie][serie.name] = serie.data[categorie];
                    }
                }
                $scope.list = list;
            });
        };

        $scope.getHomeStatusStat = function() {
            $scope.param = {
                dimension: $rootScope.DIMENSION_COLLECTION,
                statType: $rootScope.STAT_TYPE_HOUR,
                startTime: new Date(new Date().getTime() - 24 * 3600 * 1000).getDayBeginTime(),
                endTime: new Date(new Date().getTime() - 24 * 3600 * 1000).getDayEndTime()
            };
            statService.getStatusStat($scope.param, function (stat) {
                if ($scope.homeChart != null) {
                    $scope.homeChart.destroy();
                }
                $scope.homeChart = $scope.reloadChart('statusChart', '状态统计', stat);
            });
        };
    }]);
});